package com.huitongmaoyi.ai.dao;

import com.huitongmaoyi.ai.entity.Customer;
import com.huitongmaoyi.ai.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class CustomerDAO {

    /**
     * 获取所有客户（不分页）
     */
    public List<Customer> getAllCustomers() {
        List<Customer> customers = new ArrayList<>();
        String sql = "SELECT id, name, country_code, city, industry, annual_order_value, " +
                "last_contact_days, contact_person, email, phone, address, " +
                "payment_terms, credit_rating, risk_level, notes " +
                "FROM customers " +
                "ORDER BY created_at DESC";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql);
             ResultSet rs = stmt.executeQuery()) {

            while (rs.next()) {
                Customer c = new Customer(
                        rs.getString("id"),
                        rs.getString("name"),
                        rs.getString("country_code"),
                        rs.getString("city"),
                        rs.getString("industry"),
                        rs.getDouble("annual_order_value"),
                        rs.getInt("last_contact_days"),
                        rs.getString("contact_person"),
                        rs.getString("email"),
                        rs.getString("phone"),
                        rs.getString("address"),
                        rs.getString("payment_terms"),
                        rs.getString("credit_rating"),
                        rs.getInt("risk_level"),
                        rs.getString("notes")
                );
                customers.add(c);
            }
        } catch (SQLException e) {
            System.err.println("❌ 查询客户列表失败: " + e.getMessage());
            e.printStackTrace();
        }

        return customers;
    }

}